package com.zanglikun.activity;

import com.zanglikun.activity.util.*;

/**
 * 订单处理流程示例
 * 演示如何使用 Activity 模式处理复杂业务逻辑
 */
public class OrderProcessExample {


    public static void main(String[] args) {
        // 创建执行器
        ActivityExecutor executor = new ActivityExecutor();

        // 注册 Activities
        executor.register(new ValidateOrderActivity())
                .register(new CheckInventoryActivity())
                .register(new CalculatePriceActivity())
                .register(new CreateOrderActivity())
                .addListener(new SimpleActivityListener());

        // 创建上下文并执行
        ActivityContext context = new ActivityContext();
        context.put("orderId", "ORDER-001");
        context.put("productId", "PROD-001");
        context.put("quantity", 5);
        context.put("unitPrice", 99.99);

        try {
            executor.execute(context);
            System.out.println("\n✓ 订单处理完成！最终订单号: " + context.get("orderNo"));
        } catch (Exception e) {
            System.err.println("\n✗ 订单处理失败: " + e.getMessage());
        }
    }

    // 简单监听器实现
    static class SimpleActivityListener implements ActivityListener {
        @Override
        public void onBefore(Activity activity, ActivityContext context) {
            System.out.println("  └─ 开始执行: " + activity.getName());
        }

        @Override
        public void onError(Activity activity, ActivityContext context, Exception e) {
            System.err.println("  └─ 错误: " + e.getMessage());
        }
    }
}